package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户相关接口
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 条件查询用户列表  可不写添加
     * @param userVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result list(@RequestBody(required = false) UserVo userVo){
    PageInfo pageInfo = userService.list(userVo);
     return new Result("success",pageInfo);
    }

    /**
     * 修改用户信息
     * @param userVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody UserVo userVo){
        try {
            userService.edit(userVo);
            return new Result("success","修改成功");
        } catch (Exception e) {
            return new Result("fail","修改失败");
        }
    }

    /**
     * 删除用户
     * @param userVo
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody UserVo userVo){
        try {
        userService.delete(userVo);
       return new Result();
        }
         catch (Exception e){
            return new Result("fail","删除失败");
         }
    }

    @PostMapping("/resetUserPassword")
    @ResponseBody
    public Result resetpassword(@RequestBody UserVo userVo){
        userService.resetpassword(userVo);
        return new Result();
    }
}
